package cn.chiship.framework.upms.biz.system.service.impl;

import cn.chiship.framework.upms.biz.system.entity.UpmsEmailSetting;
import cn.chiship.framework.upms.biz.system.entity.UpmsEmailSettingExample;
import cn.chiship.framework.upms.biz.system.mapper.UpmsEmailSettingMapper;
import cn.chiship.framework.upms.biz.system.service.UpmsEmailSettingService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.core.id.SnowflakeIdUtil;
import cn.chiship.sdk.framework.base.BaseServiceImpl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 三方邮件发送业务接口实现层
 * 2021/9/30
 *
 * @author lijian
 */
@Service
public class UpmsEmailSettingServiceImpl extends BaseServiceImpl<UpmsEmailSetting, UpmsEmailSettingExample> implements UpmsEmailSettingService {

    @Resource
    UpmsEmailSettingMapper upmsEmailSettingMapper;


    @Override
    public BaseResult insertSelective(UpmsEmailSetting upmsEmailSetting) {
        UpmsEmailSettingExample upmsEmailSettingExample = new UpmsEmailSettingExample();
        UpmsEmailSettingExample.Criteria criteria = upmsEmailSettingExample.createCriteria();
        criteria.andTypeEqualTo(upmsEmailSetting.getType()).andOutreachInfoIdEqualTo(upmsEmailSetting.getOutreachInfoId()).andSendFromAddressEqualTo(upmsEmailSetting.getSendFromAddress());
        if (upmsEmailSettingMapper.countByExample(upmsEmailSettingExample) == 0) {
            return super.insertSelective(upmsEmailSetting);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "邮箱网关已配置，请确定信息");
        }
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(UpmsEmailSetting upmsEmailSetting) {
        UpmsEmailSettingExample upmsEmailSettingExample = new UpmsEmailSettingExample();
        UpmsEmailSettingExample.Criteria criteria = upmsEmailSettingExample.createCriteria();
        criteria.andTypeEqualTo(upmsEmailSetting.getType()).andOutreachInfoIdEqualTo(upmsEmailSetting.getOutreachInfoId()).andIdNotEqualTo(upmsEmailSetting.getId()).andSendFromAddressEqualTo(upmsEmailSetting.getSendFromAddress());
        if (upmsEmailSettingMapper.countByExample(upmsEmailSettingExample) == 0) {
            return super.updateByPrimaryKeySelective(upmsEmailSetting);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "邮箱网关已配置，请确定信息");
        }
    }

    @Override
    public BaseResult configSystemEmail(UpmsEmailSetting upmsEmailSetting) {
        upmsEmailSetting.setType(Byte.valueOf("0"));
        UpmsEmailSettingExample upmsEmailSettingExample = new UpmsEmailSettingExample();
        UpmsEmailSettingExample.Criteria criteria = upmsEmailSettingExample.createCriteria();
        criteria.andIsDeletedEqualTo(BaseConstants.NO);
        List<UpmsEmailSetting> upmsEmailSettings = upmsEmailSettingMapper.selectByExample(upmsEmailSettingExample);
        if (!upmsEmailSettings.isEmpty()) {
            upmsEmailSetting.setId(upmsEmailSettings.get(0).getId());
            upmsEmailSetting.setGmtModified(System.currentTimeMillis());
            return super.updateByPrimaryKeySelective(upmsEmailSetting);
        } else {
            upmsEmailSetting.setId(SnowflakeIdUtil.generateStrId());
            upmsEmailSetting.setGmtCreated(System.currentTimeMillis());
            upmsEmailSetting.setGmtModified(System.currentTimeMillis());
            upmsEmailSetting.setIsDeleted(BaseConstants.NO);
            return super.insertSelective(upmsEmailSetting);
        }
    }

    @Override
    public List<UpmsEmailSetting> getEmailConfigByUserIdAndType(String outreachInfoId, String type) {
        UpmsEmailSettingExample upmsEmailSettingExample = new UpmsEmailSettingExample();
        upmsEmailSettingExample.createCriteria().andIsDeletedEqualTo(BaseConstants.NO)
                .andOutreachInfoIdEqualTo(outreachInfoId).andTypeEqualTo(Byte.valueOf(type));
        return upmsEmailSettingMapper.selectByExample(upmsEmailSettingExample);

    }
}
